package fjut.acm.coding.controller.judge;

import fjut.acm.coding.entity.result.ResultCode;
import fjut.acm.coding.entity.result.ResultJson;
import fjut.acm.coding.entity.vo.StatusCountVO;
import fjut.acm.coding.entity.vo.StatusListVO;
import fjut.acm.coding.service.JudgeStatusService;
import fjut.acm.coding.service.ViewJudgeStatusService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;


@RestController
@CrossOrigin
@RequestMapping("/judge_status")
public class JudgeStatusController {
    @Resource
    JudgeStatusService judgeStatusService;

    @Resource
    ViewJudgeStatusService viewJudgeStatusService;

    @GetMapping("/list")
    public ResultJson getStatusList(@RequestParam("pageNum") Integer pageNum,
                                    @RequestParam("pageSize") Integer pageSize,
                                    @RequestParam(value = "contestId", required = false) Integer contestId,
                                    @RequestParam(value = "nickname", required = false) String nickname,
                                    @RequestParam(value = "problemId", required = false) Integer problemId,
                                    @RequestParam(value = "result", required = false) Integer result,
                                    @RequestParam(value = "language", required = false) Integer language) {
        ResultJson resultJson = new ResultJson(ResultCode.REQUIRED_SUCCESS);
        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize || pageSize > 100) {
            pageSize = 50;
        }
        if (!StringUtils.isEmpty(nickname)) {
            nickname = "%" + nickname + "%";
        } else {
            nickname = null;
        }
        List<StatusListVO> statusListVOS = viewJudgeStatusService.pagesByConditions(pageNum, pageSize, contestId, nickname, problemId, result, language);
        int count = viewJudgeStatusService.countByConditions(contestId, nickname, problemId, result, language);
        resultJson.addInfo(statusListVOS);
        resultJson.addInfo(count);
        return resultJson;
    }

    @GetMapping("/count")
    public ResultJson getStatusCountByDay(@RequestParam(value = "days", required = false) String daysStr) {
        int days;
        days = daysStr == null ? 60 : Integer.parseInt(daysStr);
        ResultJson resultJson = new ResultJson();
        HashMap<Date, StatusCountVO> hashMap = new HashMap<>(105);
        for (int i = 0; i < days; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -days + 1 + i);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            StatusCountVO statusCountVO = new StatusCountVO();
            statusCountVO.setTotalCount(0);
            statusCountVO.setAcCount(0);
            statusCountVO.setSubmitDay(calendar.getTime());
            hashMap.put(calendar.getTime(), statusCountVO);
        }
        List<StatusCountVO> statusCountVOS = judgeStatusService.selectCountByDay(days);
        for (StatusCountVO statusCountVO : statusCountVOS) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(statusCountVO.getSubmitDay());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            hashMap.put(calendar.getTime(), statusCountVO);
        }
        List<StatusCountVO> ans = new ArrayList<>();
        for (Date date : hashMap.keySet()) {
            StatusCountVO statusCountVO = hashMap.get(date);
            ans.add(statusCountVO);
        }
        //按时间升序
        Collections.sort(ans, Comparator.comparing(StatusCountVO::getSubmitDay));
        resultJson.setStatus(ResultCode.REQUIRED_SUCCESS);
        resultJson.addInfo(ans);
        return resultJson;
    }
}
